using System;
using System.Text;

public class StringUtil
{
    public enum TimeFormat
    {
        MMSS,
        HHMMSS,
        DDHHMMSS
    }

    private static readonly StringBuilder SharedStringBuilder = new StringBuilder();
    private static readonly StringBuilder internalStringBuilder = new StringBuilder();

    public static StringBuilder GetShareStringBuilder()
    {
        SharedStringBuilder.Remove(0, SharedStringBuilder.Length);
        return SharedStringBuilder;
    }

    /// <summary>
    ///     使用StringBuilder拼接字符串
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string Concat(params string[] s)
    {
        internalStringBuilder.Remove(0, internalStringBuilder.Length);
        foreach (var t in s) internalStringBuilder.Append(t);
        return internalStringBuilder.ToString();
    }

    /// <summary>
    ///     将配置表中的字符串转数字，默认返回0
    /// </summary>
    /// <param name="dataStr"></param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static T GetConfigNumber<T>(string dataStr) where T : IConvertible
    {
        try
        {
            var data = (T)Convert.ChangeType(dataStr, typeof(T));
            return data;
        }
        catch
        {
            return (T)Convert.ChangeType("0", typeof(T));
        }
    }

    /// <summary>
    ///     将秒转成倒计时显示的字符串(前后都舍弃)
    /// </summary>
    /// 3600s= 1s0m0s 返回00:00
    /// <param name="seconds"></param>
    /// <param name="format"></param>
    /// <returns></returns>
    public static string GetTimerStrBySeconds(int seconds, TimeFormat format = TimeFormat.MMSS)
    {
        var ts = new TimeSpan(0, 0, 0, seconds, 0);
        switch (format)
        {
            case TimeFormat.MMSS:
                return $"{GetDoubleDigits(ts.Minutes)}:{GetDoubleDigits(ts.Seconds)}";
            case TimeFormat.HHMMSS:
                return $"{GetDoubleDigits(ts.Hours)}:{GetDoubleDigits(ts.Minutes)}:{GetDoubleDigits(ts.Seconds)}";
            case TimeFormat.DDHHMMSS:
                return
                    $"{GetDoubleDigits(ts.Days)}:{GetDoubleDigits(ts.Hours)}:{GetDoubleDigits(ts.Minutes)}:{GetDoubleDigits(ts.Seconds)}";
            default:
                return "";
        }
    }

    /// <summary>
    ///     将毫秒转成倒计时显示的字符串(前后都舍弃)
    /// </summary>
    /// <param name="milliseconds"></param>
    /// <param name="format"></param>
    /// <returns></returns>
    public static string GetTimerStrByMilliseconds(int milliseconds, TimeFormat format = TimeFormat.MMSS)
    {
        var ts = new TimeSpan(0, 0, 0, 0, milliseconds);
        switch (format)
        {
            case TimeFormat.MMSS:
                return $"{GetDoubleDigits(ts.Minutes)}:{GetDoubleDigits(ts.Seconds)}";
            case TimeFormat.HHMMSS:
                return $"{GetDoubleDigits(ts.Hours)}:{GetDoubleDigits(ts.Minutes)}:{GetDoubleDigits(ts.Seconds)}";
            case TimeFormat.DDHHMMSS:
                return
                    $"{GetDoubleDigits(ts.Days)}:{GetDoubleDigits(ts.Hours)}:{GetDoubleDigits(ts.Minutes)}:{GetDoubleDigits(ts.Seconds)}";
            default:
                return "";
        }
    }

    /// <summary>
    ///     将小于10的整数N转化成0N的形式
    /// </summary>
    /// <param name="number"></param>
    /// <returns></returns>
    public static string GetDoubleDigits(int number)
    {
        if (number < 10) return $"0{number}";
        return number.ToString();
    }

    #region Base64加密解密

    /// <summary>
    ///     Base64加密
    /// </summary>
    /// <param name="input">需要加密的字符串</param>
    /// <returns></returns>
    public static string Base64Encrypt(string input)
    {
        return Base64Encrypt(input, new UTF8Encoding());
    }

    /// <summary>
    ///     Base64加密
    /// </summary>
    /// <param name="input">需要加密的字符串</param>
    /// <param name="encode">字符编码</param>
    /// <returns></returns>
    public static string Base64Encrypt(string input, Encoding encode)
    {
        return Convert.ToBase64String(encode.GetBytes(input));
    }

    /// <summary>
    ///     Base64解密
    /// </summary>
    /// <param name="input">需要解密的字符串</param>
    /// <returns></returns>
    public static string Base64Decrypt(string input)
    {
        return Base64Decrypt(input, new UTF8Encoding());
    }

    /// <summary>
    ///     Base64解密
    /// </summary>
    /// <param name="input">需要解密的字符串</param>
    /// <param name="encode">字符的编码</param>
    /// <returns></returns>
    public static string Base64Decrypt(string input, Encoding encode)
    {
        return encode.GetString(Convert.FromBase64String(input));
    }

    #endregion
}